package com.fengwk.resource.face.client;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fengwk.resource.face.constant.ResourceConstant;
import com.fengwk.resource.face.model.File;
import com.fengwk.support.exception.BaseException;
import com.fengwk.support.util.CollectionUtils;
import com.fengwk.support.util.HttpClientUtils;
import com.fengwk.support.util.JsonUtils;
import com.fengwk.support.util.MathUtils;
import com.fengwk.support.util.HttpClientUtils.ResponseEntity;
import com.fengwk.support.util.ResultUtils.JsonMap;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

import feign.Client;

import org.springframework.cloud.netflix.feign.ribbon.FeignLoadBalancer;
import org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient;

@Component
public class UploadClient {

	@Autowired
	private Client client;
	
	public List<File> upload(Map<String, byte[]> fileMap) {
		return upload(fileMap, null, null, null, null);
	}
	
	public List<File> upload(Map<String, byte[]> fileMap, Map<String, String> params) {
		return upload(fileMap, params, null, null, null);
	}
	
	public List<File> upload(Map<String, byte[]> fileMap, Map<String, String> params, Map<String, String> headers) {
		return upload(fileMap, params, headers, null, null);
	}
	
	public List<File> upload(Map<String, byte[]> fileMap, Map<String, String> params, Map<String, String> headers, Integer connectTimeout, Integer socketTimeout) {
		List<File> fileList = new ArrayList<File>();
		Method method = null;
		try {
			method = LoadBalancerFeignClient.class.getDeclaredMethod("lbClient", String.class);
		} catch (NoSuchMethodException | SecurityException e) {
			// ignore
		}
		
		if (method == null) {
			throw new BaseException(LoadBalancerFeignClient.class.getName() + ".lbClient调用异常");
		}
		
		try {
			method.setAccessible(true);
			FeignLoadBalancer feignLoadBalancer = (FeignLoadBalancer) method.invoke(client, ResourceConstant.FEIGN_CLIENT_NAME);
			ILoadBalancer iLoadBalancer = feignLoadBalancer.getLoadBalancer();
			List<Server> serverList = iLoadBalancer.getAllServers();
			if (CollectionUtils.isBlank(serverList)) {
				throw new BaseException("未发现可用服务");
			}
			Server server = serverList.get(MathUtils.getRandomBetweenInteger(0, serverList.size() - 1));
			String url = "http://" + server.getId() + "/api/upload";
			ResponseEntity responseEntity = HttpClientUtils.upload(url, fileMap, params, headers, connectTimeout, socketTimeout);
			JsonMap jsonMap = JsonUtils.fromJson(responseEntity.getStringContent(), JsonMap.class);
			fileList = jsonMap.getDataToList(File.class);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			e.printStackTrace();
		}
		return fileList;
	}

}
